#include "peon.h"
#include "peonstates.h"
#include <cassert>

Peon::Peon(int iID):BaseGameEntity(iID),
m_Location(townhall),
m_iGoldCarried(0),
m_iBagCapacity(10),
m_fDt(0),
m_iDestinationCol(0),
m_iDestinationRow(0),
m_fWalkingSpeed(0.15),
m_fMiningSpeed(0.1),
m_dMineStartTime(0),
m_iUp(1),
m_iDown(2),
m_iLeft(3),
m_iRight(4)
{
	EntityManager::Instance()->RegisterEntity(this);
	m_pStateMachine = new StateMachine<Peon>(this);

	m_pStateMachine->SetCurrentState(IdleState::Instance());
	m_pStateMachine->SetGlobalState(PeonGlobalState::Instance());
}
/*-------------------------------------------------------------------------------------------------------------*/
bool Peon::Initialize()
{
	if (!this->LoadIni("data/graphics/peon/peon.spr"))
	{
		return false;
	}

	if(!m_walkup.Load("data/graphics/peon/walkup.ani"))
	{
		return false;
	}

	if(!m_walkdown.Load("data/graphics/peon/walkdown.ani"))
	{
		return false;
	}

	if(!m_walkright.Load("data/graphics/peon/walkright.ani"))
	{
		return false;
	}

	if(!m_walkleft.Load("data/graphics/peon/walkleft.ani"))
	{
		return false;
	}

	return true;
}

/*-------------------------------------------------------------------------------------------------------------*/
Peon::~Peon()
{
	EntityManager::Instance()->RemoveEntity(this);
	delete m_pStateMachine;
}

/*-------------------------------------------------------------------------------------------------------------*/
void Peon::Update(float fDt)
{
	m_fDt = fDt;

	Sprite::Update(m_fDt);
	m_pStateMachine->Update();
}

/*-------------------------------------------------------------------------------------------------------------*/
void Peon::AddGold(int iGold)
{
	if(m_iGoldCarried != m_iBagCapacity)
	{
		m_iGoldCarried += iGold;
	}
}

/*-------------------------------------------------------------------------------------------------------------*/
bool Peon::IsBagFull()
{
	if(m_iGoldCarried == m_iBagCapacity)
	{
		return true;
	}

	return false;
}

/*-------------------------------------------------------------------------------------------------------------*/
void Peon::Draw()
{
	Sprite::Draw();
}

/*-------------------------------------------------------------------------------------------------------------*/
void Peon::SetAStar(AStarResult* pAStarResult)
{
	m_AStarResult.push_back(pAStarResult);
}

/*-------------------------------------------------------------------------------------------------------------*/
void Peon::SetDestination(float fPosX, float fPosY)
{
	Level::m_map.GetColRowFromWorldCoords(fPosX, fPosY, m_iDestinationCol, m_iDestinationRow);
}